package cn.kailangtu.serialutils.api;

import android.os.FileObserver;
import android.os.SystemClock;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import cn.kailangtu.commontools.AdbUtil;

public class GpioUtils {
    private static String gpio1 = "cat /sys/class/gpio/gpio3/value";
    private static String gpio2 = "cat /sys/class/gpio/gpio23/value";
    private static String gpio3 = "cat /sys/class/gpio/gpio20/value";

    private static String findGpio3 = "echo 3 > /sys/class/gpio/export";
    private static String toOut3 = "echo in > /sys/class/gpio/gpio3/direction";
    private static String toOut23 = "echo in > /sys/class/gpio/gpio23/direction";
    private static String toOut20 = "echo in > /sys/class/gpio/gpio20/direction";

    private static String chmod3 = "chmod 0666 /sys/class/gpio/gpio3/value";
    private static String chmod23 = "chmod 0666 /sys/class/gpio/gpio23/value";
    private static String chmod20 = "chmod 0666 /sys/class/gpio/gpio20/value";

    static GpioUtils instance;

    public static GpioUtils getInstance(){
        if(instance == null)
        {
            instance = new GpioUtils();

            AdbUtil.run(findGpio3);
            AdbUtil.run(toOut3);
            AdbUtil.run(toOut23);
            AdbUtil.run(toOut20);
            AdbUtil.run(chmod3);
            AdbUtil.run(chmod23);
            AdbUtil.run(chmod20);
        }
        return instance;
    }

    private GpioUtils()
    {

    }

    boolean isBegin = false;

    int lastValue3 = 0;
    int lastValue20=0;
    int lastValue23=0;

    public void beginSpy() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (!isBegin) {
                    isBegin = true;
                    boolean hasChanged = false;
                    while (isBegin) {
                        hasChanged = false;
                        int currValue3 = readGpioValue("gpio3");
                        if(lastValue3 != currValue3)
                        {
                            lastValue3 = currValue3;
                            hasChanged = true;
                        }
                        SystemClock.sleep(5);
                        int currValue23 = readGpioValue("gpio23");
                        if(lastValue23 != currValue23)
                        {
                            lastValue23 = currValue23;
                            hasChanged = true;
                        }

                        SystemClock.sleep(5);
                        int currValue20 = readGpioValue("gpio20");
                        if(lastValue20 != currValue20)
                        {
                            lastValue20 = currValue20;
                            hasChanged = true;
                        }

                        if(hasChanged)
                        {
                            DIManager.getInstance().newValue(lastValue3,lastValue23,lastValue20);
                        }
                        SystemClock.sleep(5);
                    }
                    isBegin = false;
                }
            }
        }).start();

    }


    private int readGpioValue(String add)
    {
        int result = 0;
        try {
            Process process = Runtime.getRuntime().exec("cat /sys/class/gpio/"+add+"/value");
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(process.getInputStream()))) {
                String value = br.readLine();
                if(value.equals("0"))
                {
                    result = 1;
                }
                else if(value.equals("1"))
                {
                    result = 0;
                }
                Log.d("GPIO VALUE MODIFY", value);
            } catch (Exception e) {
            }
        }
        catch(Exception eeeee){}
        return result;
    }





}
